package com.gf.snoopy.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.gf.snoopy.platform.exception.PokeErrorCode;
import com.gf.snoopy.platform.exception.PokeException;
import com.gf.snoopy.util.page.PagedList;

public class SnoopyUtil {
	
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd:HHmmSS");
	
	public static final String SUCCESS = "ok";
	
	public static final String tableDataName = "aaData";
	
	public static final String tableDataTotal = "iTotalRecords";

	public static final String tableDataTotalDisplay = "iTotalDisplayRecords";
	
	public static final String tableDataStart = "iDisplayStart";
	
	public static final String tableDataPageSize = "iDisplayLength";
	
	public static final String tableSearch = "sSearch";
	
	public static final String tableDataKey = "name";
	
	public static final String tableDataValue = "value";
	
	private static final Logger logger = Logger.getLogger(SnoopyUtil.class);
	
	/**
	 * 将PagedList构造成DataTable的返回格式
	 * @param pageList
	 * @return
	 */
	public static Map<String,Object> constructPageResult(PagedList pageList){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put(tableDataTotal, pageList.getPage().getTotal());
		result.put(tableDataTotalDisplay, pageList.getPage().getTotal());
		result.put(tableDataName, pageList.getList());
		return result;
	}
	
	/**
	 * 解析dataTable的输入
	 * @param aoData
	 * @return
	 */
	public static AoDataPage parseAoData(String aoData){
		AoDataPage page = new AoDataPage();
		JSONArray jList = JSONArray.fromObject(aoData);
		for(int i=0;i<jList.size();i++){
			JSONObject data = (JSONObject)jList.get(i);
			if(tableDataStart.equals(data.getString(tableDataKey))){
				page.offset = data.getInt(tableDataValue);
			}
			if(tableDataPageSize.equals(data.getString(tableDataKey))){
				page.pageSize = data.getInt(tableDataValue);
			}
			if(tableSearch.equals(data.getString(tableDataKey))){
				if(StringUtils.isNotBlank(data.getString(tableDataValue))){
					page.search = data.getString(tableDataValue);
				}				
			}
		}
		return page;
	}
	
	public static void write(OutputStream outputStream,InputStream inputStream) throws IOException{
		int byteData;
		while ((byteData = inputStream.read()) != -1){
			outputStream.write(byteData);
		}
		inputStream.close();
		outputStream.close();
	}
	
	public static byte[] convertFileToByte(File file) throws PokeException {
		if(file==null){
			return new byte[0];
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			FileInputStream in = new FileInputStream(file);
			int byteData;
			while ((byteData = in.read()) != -1) {
				out.write(byteData);
			}
			in.close();
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"转换文件出错");
		}
		return out.toByteArray();
	}
}
